package com.ikun.yummy.sourceop.op;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.ikun.yummy.bean.CookInfo;
import com.ikun.yummy.properties.AppProperties;
import com.ikun.yummy.sourceop.sqlutils.SqliteHelper;

import java.util.ArrayList;
import java.util.List;
public class CookOP {
    private final SQLiteDatabase db;
    public CookOP(Context context){
        db = new SqliteHelper(context).getReadableDatabase();
    }
    public void insert(CookInfo cookInfo){
        db.beginTransaction();
        ContentValues values = new ContentValues();
        values.put("cookname", cookInfo.cookname);
        values.put("username", cookInfo.username);
        values.put("coverimage", cookInfo.coverimage);
        values.put("ingredients", cookInfo.ingredients);
        values.put("step", cookInfo.step);
        values.put("time", cookInfo.time);
        values.put("difficulty", cookInfo.difficulty);
        values.put("taste", cookInfo.taste);
        values.put("classification", cookInfo.classification);
//        values.put("userimage", cookInfo.userimage);
        values.put("cookstory", cookInfo.cookstory);
        db.insert(AppProperties.COOK_TABLE, null, values);
        db.setTransactionSuccessful();
        db.endTransaction();
    }
    public List<CookInfo> queryAll(){
        Cursor cursor = db.query(AppProperties.COOK_TABLE,
                null,
                null,
                null,
                null,
                null,
                null,
                null);
        if (cursor != null && cursor.getCount() > 0) {
            ArrayList<CookInfo> list = new ArrayList<>();
            while (cursor.moveToNext()){
                list.add(new CookInfo(
                        cursor.getString(0),
                        cursor.getString(3),
                        cursor.getString(1),
                        cursor.getString(2),
                        cursor.getString(4),
                        cursor.getString(5),
                        cursor.getString(6),
                        cursor.getString(7),
                        cursor.getString(8),
                        cursor.getString(9),
                        cursor.getString(10),
                        "",
                        false,
                        null));
            }
            return list;
        }else{
            return null;
        }
    }
    public List<CookInfo> queryByUsername(String username){
        String sql="SELECT * FROM " + AppProperties.COOK_TABLE + " WHERE username='" + username + "'";
        Cursor cursor = db.rawQuery(sql, null, null);
        if (cursor != null && cursor.getCount() > 0) {
            ArrayList<CookInfo> list = new ArrayList<>();
            while (cursor.moveToNext()){
                list.add(new CookInfo(cursor.getString(0), cursor.getString(3), cursor.getString(1), cursor.getString(2), cursor.getString(4), cursor.getString(5), cursor.getString(6), cursor.getString(7), cursor.getString(8), cursor.getString(9), cursor.getString(10), "", false, null));
            }
            return list;
        }else{
            return null;
        }
    }
    //通过分类，关键字来查询,模糊查询
    @SuppressLint("Recycle")
    public List<CookInfo> queryBlur(String key, String type){
        Cursor cursor;
        if (type.equals("全部")){
            if (key==null||key.equals("")){
                cursor = db.rawQuery("SELECT * FROM "+AppProperties.COOK_TABLE,null,null);
            }else{
                cursor = db.rawQuery("SELECT * FROM "+AppProperties.COOK_TABLE+" WHERE cookname LIKE '%"+key+"%'",null,null);
            }
        }else{
            if (key==null||key.equals("")){
                cursor = db.rawQuery("SELECT * FROM "+AppProperties.COOK_TABLE+" WHERE classification='"+type+"'",null,null);
            }else{
                cursor = db.rawQuery("SELECT * FROM "+AppProperties.COOK_TABLE+" WHERE cookname LIKE '%"+key+"%' AND classification='"+type+"'",null,null);
            }
        }
        if (cursor != null && cursor.getCount() > 0) {
            ArrayList<CookInfo> list = new ArrayList<>();
            while (cursor.moveToNext()){
                list.add(new CookInfo(cursor.getString(0), cursor.getString(3), cursor.getString(1), cursor.getString(2), cursor.getString(4), cursor.getString(5), cursor.getString(6), cursor.getString(7), cursor.getString(8), cursor.getString(9), cursor.getString(10), "", false, null));
            }
            return list;
        }else{
            return null;
        }
    }
    public void delete(String cookid) {
        db.delete(AppProperties.COOK_TABLE, "cookid=?",new String[]{cookid});
    }
    public CookInfo queryByCookId(String cookid){//查一条数据
        @SuppressLint("Recycle") Cursor cursor = db.rawQuery("SELECT * FROM " + AppProperties.COOK_TABLE + " WHERE cookid=" + cookid, null, null);
        if (cursor!=null&&cursor.getCount()>0){
            while (cursor.moveToNext()){
                return new CookInfo(cursor.getString(0), cursor.getString(3), cursor.getString(1), cursor.getString(2), cursor.getString(4), cursor.getString(5), cursor.getString(6), cursor.getString(7), cursor.getString(8), cursor.getString(9), cursor.getString(10), "", false, null);
            }
        }
        return null;
    }
}